/*
 * ledset.h
 *
 *  Created on: 2021年12月10日
 *      Author: t1019
 */

#ifndef LEDSET_H_
#define LEDSET_H_

#include "stdint.h"
#include "stm32l0xx_hal.h"

namespace bt_led {

typedef enum
{
	SLED_OFF = 0u,
	SLED_ON = 1u,
	SLED_SLOW = 2u,
	SLED_FAST = 3u,
	SLED_BLINK = 4u
}e_LEDAction;

typedef struct
{
	GPIO_TypeDef* gpio;
	uint32_t pin;
}s_SingleLed;

class LedSet {
public:
	LedSet(const s_SingleLed* led_info, bool type_invert = false);
	static void handler(uint16_t period);
	void set(e_LEDAction mode, uint16_t timeout = 0, e_LEDAction nextmode = SLED_OFF);
	virtual ~LedSet();
private:
	void _exec(uint16_t period);
	inline void _led_set(const s_SingleLed *p,bool val)
	{
		_port_gpioset(*p,invert?(!val):(val));
	}
	inline void _port_gpioset(const s_SingleLed p,bool val)
	{
		HAL_GPIO_WritePin(p.gpio,p.pin,val?GPIO_PIN_SET:GPIO_PIN_RESET);
	}
	static LedSet *head;
	const s_SingleLed* led;
	LedSet *next;
	bool invert;
	e_LEDAction status,nextstatus;
	uint16_t change_timeout;
	uint16_t blink_timeout;

	const uint16_t fast_t = 200;
	const uint16_t slow_t = 800;
	const uint16_t blink_on = 300;
	const uint16_t blink_off = 1500;
};

} /* namespace bt_led */

#endif /* LEDSET_H_ */
